package com.atguigui.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 905.按奇偶排序数组
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/4/28 8:46
 */
public class P905SortArrayByParity {
    public static void main(String[] args) {
        Solution solution = new P905SortArrayByParity().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] sortArrayByParity(int[] nums) {
            //偶数列表
            List<Integer> evenNumber = new ArrayList();
            //基数列表
            List<Integer> cardinalNumber = new ArrayList();

            //偶数放偶数列表，基数放基数列表
            for (int num : nums) {
                if (num % 2 == 0) {
                    evenNumber.add(num);
                } else {
                    cardinalNumber.add(num);
                }
            }

            //两个合并
            evenNumber.addAll(cardinalNumber);
            //List转Integer[]
            Integer[] integers = evenNumber.toArray(new Integer[]{});
            //Integer[]转int[]
            int[] ints = Arrays.stream(integers).mapToInt(Integer::valueOf).toArray();

            return ints;
        }
    }
}
